home *** CD-ROM | disk | FTP | other *** search
- //--------------------------------------------------------------------------
- //
- // Copyright (c) 2002, Colin Granville
- //
- // All rights reserved.
- //
- // Redistribution and use in source and binary forms, with or
- // without modification, are permitted provided that the following
- // conditions are met:
- //
- // * Redistributions of source code must retain the above copyright
- // notice, this list of conditions and the following disclaimer.
- //
- // * Redistributions in binary form must reproduce the above
- // copyright notice, this list of conditions and the following
- // disclaimer in the documentation and/or other materials
- // provided with the distribution.
- //
- // * The name Colin Granville may not be used to endorse or promote
- // products derived from this software without specific prior
- // written permission.
- //
- // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
- // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
- // OF THE POSSIBILITY OF SUCH DAMAGE.
- //
- //--------------------------------------------------------------------------
-
- #include "Document.h"
- #include "DocView.h"
- #include <stdlib.h>
- #include <stdio.h>
- #include "GuiHourglass.h"
- #include "strstream.h"
- #include "error.h"
- #include "Page.h"
- #include "link.h"
- #include "Stream.h"
- #include "fstream.h"
- #include "file.h"
- #include "password.h"
- #include "Outline.h"
- #include "ErrorCodes.h"
-
- EraseScrapFile::~EraseScrapFile()
- {
- int pos=filename.find("!Scrap.ScrapDirs.ScrapDir.");
- if (pos != string::npos)
- {
- if (filename.find('.',pos+26)==string::npos) remove(filename.c_str());
- }
- }
-
- //*************************************************************************
- //*************************************************************************
- //*************************************************************************
-
- DEFINE_RTTI_DERIVED(Document,Node);
-
- bool Document::hasChildNodes() {return 1;}
-
- Document* makeDocument(Node& application,const char* file_name)
- {
- if (file_name)
- {
- Node* n;
- for (n=application.getFirstChild();n;n=n->getNextSibling())
- {
- Document* doc=PTR_dynamic_cast(Document,n);
- if (doc && doc->fileName==file_name)
- {
- return doc;
- }
- }
-
- ifstream in(file_name);
- if (!in)
- {
- error(0,"Couldn't open file '%s'",file_name);
- return 0;
- }
- in.close();
-
- Document* doc=new Document(application,file_name);
- if (doc && doc->isOk()) return doc;
- delete doc;
- }
- return 0;
- }
-
- //*************************************************************************
-
-
- Document::Document(Node& app,const char* file_name)
- : fileName(file_name),
- eraseScrapFile(file_name),
- doc(0),
- outline(0)
- {
- {
- GuiHourglass hourglass;
- doc=new PDFDoc(new GString(file_name));
- }
- if (!doc) return;
-
- if (doc->getErrorCode() == errEncrypted)
- {
- delete doc;
- doc=0;
-
- int i;
- for (i=0;i<3;i++)
- {
- string user,owner;
-
- if (!getPassword(user,owner)) break;
-
- {
- GuiHourglass hourglass;
- doc=new PDFDoc(new GString(fileName.c_str()),
- new GString((char*)owner.c_str()),
- new GString((char*)user.c_str()));
- }
- if (doc && !isOk()) {delete doc;doc=0;}
- if (isOk()) break;
- }
- }
-
- if (isOk())
- {
- outline=new Outline(getPDFDoc());
- FileInfo_create(fileInfoData,getPDFDoc(),fileName);
- app.addChild(this);
- }
- }
-
- //*************************************************************************
-
- Document::~Document()
- {
- delete outline;
- delete doc;
- }
-
- //*************************************************************************
-
- Node *Document::removeChild(Node* n)
- {
- if (!Node::removeChild(n)) return 0;
- if (getFirstChild()==0)
- delete this;
- else
- if (PTR_dynamic_cast(DocView,n)) setViewTitles();
- return n;
- }
-
- //*************************************************************************
-
- Node *Document::addChild(Node* n)
- {
- if (!Node::addChild(n)) return 0;
- if (PTR_dynamic_cast(DocView,n)) setViewTitles();
- return n;
- }
-
- //*************************************************************************
-
- void Document::setViewTitles()
- {
- int count=0;
- Node* n;
- for (n=getFirstChild();n;n=n->getNextSibling())
- {
- if (PTR_dynamic_cast(DocView,n)) count++;
- }
-
- if (!count) return;
-
- char buf[256];
- ostrstream out(buf,sizeof(buf));
- out << (fileInfoData.title.size() ? fileInfoData.title : fileName);
- if (count>1) out << " - " << count;
-
- DocView* view;
- for (n=getFirstChild();n;n=n->getNextSibling())
- {
- view=PTR_dynamic_cast(DocView,n);
- if (view) view->setTitle(out.str());
- }
- }
-
- //*************************************************************************
-
- void Document::getPage(int num,OutputDev* out,int dpi,int rotate)
- {
- getPDFDoc().displayPage(out,num,(double)dpi,rotate,0);
- }
-
- //*************************************************************************
-
- Links* Document::makeLinks(int page)
- {
- Object obj;
- Links* links=new Links(getCatalog()->getPage(page)->getAnnots(&obj),
- getCatalog()->getBaseURI());
- obj.free();
- return links;
- }
-
-